#include "imageproccesor.h"
#include <QFileDialog>
#include <QTextStream>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>

ImageProccesor::ImageProccesor(QWidget *parent)
	: QMainWindow(parent)
{
	setWindowTitle("Easy World");
	setMinimumSize(500,300);
	showwidget = new ShowWidget(this);
	setCentralWidget(showwidget);
	CreateAction();
	CreateMenus();
	CreateToolBar();
	// 将showwidget设为中心部件

}

ImageProccesor::~ImageProccesor()
{
}

void ImageProccesor::CreateAction(){
	// 打开文件
	openFileAction = new QAction(QIcon(":/new/prefix1/open.png"),tr("打开"),this);
	openFileAction->setShortcut(tr("Ctrl+O"));     // 设置组合键
	openFileAction->setStatusTip(tr("打开一个文件"));// 设置提示词
	connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));

	// 新建文件
	NewFileAction = new QAction(QIcon(":/new/prefix1/new.png"),tr("新建"));
	NewFileAction->setShortcut(tr("Ctrl+N"));
	NewFileAction->setStatusTip(tr("新建一个文件"));
	connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));
	// 打印文本
	PrintTextAction = new QAction(tr("打印文本"));
	PrintTextAction->setStatusTip(tr("打印一个文本"));
	connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText()));
	// 打印图像
	PrintImageAction = new QAction(QIcon(":/new/prefix1/printImage.png"),tr("打印图像"));
	PrintImageAction->setStatusTip(tr("打印一幅图片"));
	connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage()));
}

void ImageProccesor::CreateMenus(){
	menubar = new QMenuBar();
	FileMenu = new QMenu(tr("文件")); // 文件菜单
	FileMenu->addAction(openFileAction);
	FileMenu->addAction(NewFileAction);
	FileMenu->addAction(PrintTextAction);
	FileMenu->addAction(PrintImageAction);

	menubar->addMenu(FileMenu);
	this->setMenuBar(menubar);
}
void ImageProccesor::CreateToolBar(){
	FileToolBar = addToolBar("File");
	FileToolBar->addAction(openFileAction);
	FileToolBar->addAction(NewFileAction);
	FileToolBar->addAction(PrintImageAction);
}

void ImageProccesor::ShowNewFile(){
	ImageProccesor * newImageproccesor=new ImageProccesor;
	newImageproccesor->show();
}

void ImageProccesor::ShowOpenFile(){
	FileName = QFileDialog::getOpenFileName();
	if(!FileName.isEmpty()){
		if(showwidget->text->document()->isEmpty()){
			LoafFile(FileName);
		}else{
			ImageProccesor * newImageproccesor=new ImageProccesor;
			newImageproccesor->show();
			newImageproccesor->LoafFile(FileName);
		}
	}
}

void ImageProccesor::LoafFile(QString filename){
	QFile file(filename);
	if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
		QTextStream textstream(&file);
		while(!textstream.atEnd()){
			showwidget->text->append(textstream.readLine());
		}
	}
}

void ImageProccesor::ShowPrintText(){
	QPrinter print;
	QPrintDialog printDialog(&print,this);
	if(printDialog.exec()){
		// 获得QTextEdit对象的文档
		QTextDocument *doc=showwidget->text->document();
		doc->print(&print);//打印
	}
}
void ImageProccesor::ShowPrintImage(){
	QPrinter printer;
	QPrintDialog printDialog(&printer,this);
	if(printDialog.exec()){
		QPainter painter(&printer);
		QRect rect=painter.viewport();
		QSize size=img.size();
		size.scale(rect.size(),Qt::KeepAspectRatio);
		painter.setViewport(rect.x(),rect.y(),size.width(),size.height());
		painter.setWindow(img.rect());
		painter.drawImage(0,0,img);
	}
}
